/*
 * Singularity Engine
 * Copyright (C) 2010 Vyacheslav Vorobyov, All rights reserved.
 * See LICENSE for full license information.
 */

/**
 * 
 */
package org.singularity.io;


/**
 * @author vjache
 *
 */
public abstract class AbstractDME<T> implements DME<T> {

	protected final T _value;

	/**
	 * 
	 */
	protected AbstractDME(T aValue) {
		_value = aValue;
	}

	/* (non-Javadoc)
	 * @see org.singularity.mapred.DME#getValue()
	 */
	@Override
	public T getValue() {
		return _value;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof DME) || obj==null)
			return false;
		
		final DME<T> dme = (DME<T>)obj;
		
		return getCode().equals(dme.getCode()) && 
		(_value !=null?_value.equals(dme.getValue()):dme.getValue()==null);
	}
	
	@Override
	public int hashCode() {
		return getCode().hashCode()^(_value!=null?_value.hashCode():0);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("{code: ").append(getCode().name()).append(", value: ").append(_value).append("}");
		return sb.toString();
	}
}
